﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using App.Core.Extensions;
using App.Data;
using System.IO;


public partial class Pages_Admin_SubscriptionChange : System.Web.UI.Page
{
    private int _CustomerID = 0;
    private int _SubscriptionID = 0;

    protected void Page_Load(object sender, EventArgs e)
    {
        BindPageInfo();
        if (!IsPostBack)
        {
            
            BindDropdownList.SubscriptionTypes(ddlPackageType);
            BindSubcriptionInfo();
        }
    }

    private void BindSubcriptionInfo()
    {
        BtcHousingEntities context = new BtcHousingEntities();
        PackageSubscription packageSubscription = context.PackageSubscriptions.FirstOrDefault(P => P.ID == _SubscriptionID && P.CustomerID == _CustomerID);

        if (packageSubscription == null)
        {
            ShowErrorMessage();
        }
        else {
            Customer customer = context.Customers.FirstOrDefault(P => P.ID == _CustomerID);
            ddlPackageType.SetSelectedItem(packageSubscription.PackageID.ToString());
            chkIsActive.Checked = packageSubscription.IsActiveSubscription;
            lblCustomerName.Text = customer.Name;
        }
       
    }

    protected void BindPageInfo()
    {
        Page.Title = WebUtil.GetPageTitle(h4Heading.InnerHtml);
        _SubscriptionID = WebUtil.GetQueryStringInInt(AppConstants.QueryString.ID);
        _CustomerID = WebUtil.GetQueryStringInInt(AppConstants.QueryString.CUSTOMER_ID);

    }

    protected void SaveSubscription() {
        BtcHousingEntities context = new BtcHousingEntities();
        PackageSubscription packageSubscription = context.PackageSubscriptions.FirstOrDefault(P => P.ID == _SubscriptionID && P.CustomerID == _CustomerID);
        if (packageSubscription != null) {
            int packageID = ddlPackageType.SelectedValue.ToInt();
            var package = context.Packages.FirstOrDefault(P => P.ID == packageID && P.IsActive);
            packageSubscription.IsActiveSubscription = true;
            packageSubscription.PackageID = packageID;
            packageSubscription.ExpireDate = DateTime.Now.AddDays(package.Duration);
            packageSubscription.ProductCount = package.ProductLimit;
            packageSubscription.StartDate = DateTime.Now;
            context.SaveChanges();
        }

        _SubscriptionID = packageSubscription.ID;
        _CustomerID = packageSubscription.CustomerID;

        WebUtil.ShowMessageBox(divMessage, "Subscription Saved Successfully.", false);
    }

    protected void ShowSuccessMessage()
    {
        if (String.Compare(WebUtil.GetQueryStringInString(AppConstants.QueryString.SUCCESS_MESSAGE), "True", false) == 0)
        {
            WebUtil.ShowMessageBox(divMessage, "Advertisement Saved Successfully.", false);
        }
    }
    /// <summary>
    /// Shows a Message in the UI and Hides the Data Editing Controls
    /// </summary>
    protected void ShowErrorMessage()
    {
        pnlFormContainer.Visible = false;
        WebUtil.ShowMessageBox(divMessage, "Requested Advertisement was not found.", true);
    }

    protected void btnSave_Click(object sender, EventArgs e)
    {
        if (Page.IsValid)
        {
            SaveSubscription();
            //Response.Redirect(AppConstants.Pages.USER_LIST);
            return;
        }
    }
    protected void btnList_Click(object sender, EventArgs e)
    {
        //Response.Redirect(AppConstants.Pages.USER_LIST);
        //return;
    }
}